#include "Geometry.fxh"

float3 g_LightPos_Local;
float g_LightRangeSq;

struct VS_TO_PS
{
	float4 Pos : POSITION;
	#if VERTEX_COLOR != 0
		float4 Color : COLOR0;
	#endif
	#ifdef USE_TEXCOORD
		float2 TexCoord : TEXCOORD0;
	#endif

	float3 VecFromLight_World : TEXCOORD1;
};

void VS(in APP_TO_VS In, out VS_TO_PS Out)
{
	float3 localPos = PreprocessLocalPos(In);
	Out.Pos = mul(float4(localPos, 1), g_WorldViewProj);
	#if VERTEX_COLOR != 0
		Out.Color = In.Color;
	#endif
	#ifdef USE_TEXCOORD
		Out.TexCoord = CalcTexCoord(In);
	#endif

	float3 vecFromLight_Local = localPos - g_LightPos_Local;
	Out.VecFromLight_World = mul(vecFromLight_Local, (float3x3)g_World);
}

void PS(in VS_TO_PS In, out float4 Out : COLOR0)
{
	Out = dot(In.VecFromLight_World, In.VecFromLight_World) / g_LightRangeSq;

	#if ALPHA_TEST != 0
		float alpha = 1;
		#if CONSTANT_COLOR != 0
			alpha *= g_ConstantColor.a;
		#endif
		#if VERTEX_COLOR != 0
			alpha *= In.Color.a;
		#endif
		#if TEXTURE != 0
			alpha *= tex2D(DiffuseSampler, In.TexCoord).a;
		#endif
		clip(alpha - g_AlphaTestRef);
	#endif
}

technique {
	pass {
		VertexShader = compile vs_3_0 VS();
		PixelShader  = compile ps_3_0 PS();
	}
}

